﻿#include "qtmain.h"

qtMain::qtMain(QWidget* parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
    this->setupUi2();
}

qtMain::~qtMain() {}

void qtMain::setupUi2()
{
    context.reset(new ApplicationContext(this));
    context->setGlobal(new AppGlobal(context));
    this->setWindowTitle(context->ProductName);
    this->setWindowIcon(*QtUtil::createIcon(
        context->applicationDirectory->getAppFile("appIcon.svg")
    ));
    this->setAutoFillBackground(true);

    setupMenuBar();
    setupToolBar();
    setupStatusBar();
    setupLayout();
    setupPropertyPane();
    setupCenterTab();
    setupTabRecognitionAuto();
    setupTabReport();
    renderTitle();
    showMaximized();
    QWidget::setWindowFlags(
        Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint |
        Qt::WindowMinimizeButtonHint
    );
}

void qtMain::setupMenuBar()
{
    // 文件
    menuFile = QtUtil::createMenu(
        ui.menuBar, "menuFile", QString::fromStdWString(L"文件(&F)")
    );
    menuFile->addAction(ActionManager::get(context)->fileLoadSettingsAction);
    QtUtil::createMenuSeparator(menuFile);
    menuFile->addAction(ActionManager::get(context)->fileExitAction);

    // 功能
    QMenu* menuData = QtUtil::createMenu(
        ui.menuBar, "menuData", QString::fromStdWString(L"功能")
    );
    menuData->addAction(ActionManager::get(context)->opStartAction);
    menuData->addAction(ActionManager::get(context)->opStopAction);
    menuData->addAction(ActionManager::get(context)->recognitionAreaConfigAction
    );
    menuData->addAction(ActionManager::get(context)->fileExportAction);
    QtUtil::createMenuSeparator(menuData);
    menuData->addAction(ActionManager::get(context)->photoListAction);
    menuData->addAction(ActionManager::get(context)->workpieceEditAction);

    // 帮助
    QMenu* menuHelp = QtUtil::createMenu(
        ui.menuBar, "menuHelp", QString::fromStdWString(L"帮助(&H)")
    );
    menuHelp->addAction(ActionManager::get(context)->helpAboutAction);
}

void qtMain::setupToolBar()
{
    // 文件
    ui.mainToolBar->addAction(ActionManager::get(context)->opStartAction);
    ui.mainToolBar->addAction(ActionManager::get(context)->opStopAction);
    ui.mainToolBar->addAction(
        ActionManager::get(context)->recognitionAreaConfigAction
    );
    ui.mainToolBar->addAction(ActionManager::get(context)->fileExportAction);

    // 数据处理
    ui.mainToolBar->addSeparator();
    ui.mainToolBar->addAction(ActionManager::get(context)->photoListAction);
    ui.mainToolBar->addAction(ActionManager::get(context)->workpieceEditAction);
}

void qtMain::setupStatusBar()
{
    // 状态栏
    statusBarManager = new MainStatusBarManager(ui.statusBar, context);
}

void qtMain::setupLayout()
{
    this->mainLayout = new QVBoxLayout(ui.centralWidget);
    this->mainLayout->setObjectName("mainLayout");
}

void qtMain::setupPropertyPane()
{
    // QDockWidget *dock = new QDockWidget(QString::fromStdWString(L"参数"),
    // this); dock->setFixedWidth(_paneWidth);
    // dock->setAllowedAreas(Qt::LeftDockWidgetArea);
    // dock->setFeatures(QDockWidget::NoDockWidgetFeatures);
    // addDockWidget(Qt::LeftDockWidgetArea, dock);

    //// PropertyEditWidget* propertyEdit = new PropertyEditWidget(dock,
    ///context, / true); dock->setWidget(propertyEdit);

    //// context->global()->setWorkpieceEditWidget(propertyEdit);
    // QtTreePropertyBrowser *propertyEditor = new QtTreePropertyBrowser(dock);
    // dock->setWidget(propertyEditor);
}

void qtMain::setupCenterTab()
{
    this->tabCenter =
        QtUtil::createTabWidget(this->mainLayout->parentWidget(), "tabCenter");
    this->mainLayout->addWidget(this->tabCenter);
    this->tabCenter->setSizePolicy(
        QSizePolicy::Expanding, QSizePolicy::Expanding
    );
}

void qtMain::setupTabRecognitionAuto()
{
    // 自动识别
    this->tabRecognitionAuto = QtUtil::createTab(
        this->tabCenter, "tabRecognitionAuto",
        QString::fromStdWString(L"自动识别")
    );
    QVBoxLayout* layout = new QVBoxLayout(this->tabRecognitionAuto);
    this->viewRecognitionAuto =
        new RecognitionAutoWidget(this->tabRecognitionAuto, this->context);
    layout->addWidget(this->viewRecognitionAuto);
    context->global()->setRecongitionOperationWidget(this->viewRecognitionAuto);
}

void qtMain::setupTabReport()
{
    //// 报表
    // this->tabReportTableWidget = QtUtil::createTab(this->tabCenter,
    // "tabReportTableWidget", QString::fromStdWString(L"报表")); QVBoxLayout*
    // layout = new QVBoxLayout(this->tabReportTableWidget);
    // this->viewReportTable = new ReportTableWidget(this->tabReportTableWidget,
    // this->context); layout->addWidget(this->viewReportTable);

    // this->tabReportWidget = QtUtil::createTab(this->tabCenter,
    // "tabReportWidget", QString::fromStdWString(L"报表详细")); layout = new
    // QVBoxLayout(this->tabReportWidget); this->viewReport = new
    // ReportWidget(this->tabReportWidget, this->context);
    // layout->addWidget(this->viewReport);
}

void qtMain::renderTitle()
{
    context->global()->renderTitle();
}

void qtMain::closeEvent(QCloseEvent* e)
{
    context->close();
}
